package com.atguigu.daijia.customer.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.customer.client.CustomerInfoFeignClient;
import com.atguigu.daijia.customer.service.CustomerService;
import com.atguigu.daijia.model.entity.customer.CustomerInfo;
import com.atguigu.daijia.model.form.customer.UpdateWxPhoneForm;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerInfoFeignClient customerInfoFeignClient;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public String login(String code) {
        log.info(">>> 1 >>>>web-customer服务=>远程调用customer服务，用code进行微信登录");
        Result<Long> result = customerInfoFeignClient.login(code);
        if(result.isFail()){
            log.info("web-customer服务调用 service-customer服务 失败");
            throw new GuiguException(ResultCodeEnum.FEIGN_RPC_FAIL);
        }
        Long customerId = result.getData();
        log.info(">>>> 2 >>>> 获取用户的customerId =>"+customerId);
        if(null ==customerId){
            log.error("获取customerId异常");
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        log.info(">>>> 3 >>>> 生成token=>{}",token);
        redisTemplate.opsForValue().set(
                RedisConstant.USER_LOGIN_KEY_PREFIX+token,
                customerId.toString(),
                RedisConstant.USER_LOGIN_KEY_TIMEOUT,
                TimeUnit.SECONDS);
        log.info(">>>> 4 >>>> 将用户登录信息放入到redis，key是token，value是customerId，过期时间是30分钟");
        return token;
    }

    /**
     * 获取用户登录信息
     * @return 用户登录信息
     */
    @Override
    public CustomerLoginVo getCustomerLoginInfo(String token) {
        log.info("拿token从redis里获取用户的customerId");
        String customerId = (String) redisTemplate.opsForValue().get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);
        if(null == customerId){
            throw new GuiguException(ResultCodeEnum.SIGN_OVERDUE);
        }
        log.info("拿customerId调用service-customer服务 获取用户的基本信息");
        Result<CustomerInfo> result = customerInfoFeignClient.getCustomerInfo(Long.parseLong(customerId));
        if(result.isFail() ){
            log.error("rpc远程调用异常");
            throw new GuiguException(ResultCodeEnum.FEIGN_RPC_FAIL);
        }
        if(null == result.getData()){
            log.error("获取rpc返回的数据异常");
            throw new GuiguException(ResultCodeEnum.DATA_GET_ERRPR);
        }
        log.info("拼装前端数据");
        CustomerLoginVo customerLoginVo = new CustomerLoginVo();
        BeanUtils.copyProperties(result.getData(),customerLoginVo);
        customerLoginVo.setIsBindPhone(result.getData().getPhone() !=null);

        return customerLoginVo;
    }

    @Override
    public CustomerLoginVo getCustomerLoginInfoNew(Long userId) {
        if(null == userId){
            throw new GuiguException(ResultCodeEnum.SIGN_OVERDUE);
        }
        log.info("拿customerId调用service-customer服务 获取用户的基本信息");
        Result<CustomerInfo> result = customerInfoFeignClient.getCustomerInfo(userId);
        if(result.isFail() ){
            log.error("rpc远程调用异常");
            throw new GuiguException(ResultCodeEnum.FEIGN_RPC_FAIL);
        }
        if(null == result.getData()){
            log.error("获取rpc返回的数据异常");
            throw new GuiguException(ResultCodeEnum.DATA_GET_ERRPR);
        }
        log.info("拼装前端数据");
        CustomerLoginVo customerLoginVo = new CustomerLoginVo();
        BeanUtils.copyProperties(result.getData(),customerLoginVo);
        customerLoginVo.setIsBindPhone(result.getData().getPhone() !=null);
        log.info("返回给前端");
        return customerLoginVo;
    }

    @Override
    public Boolean updateWxPhoneNumber(UpdateWxPhoneForm updateWxPhoneForm) {
        customerInfoFeignClient.updateWxPhoneNumber(updateWxPhoneForm);
        //这里永远返回true是因为 个人用户调获取微信手机号的接口是没有权限的这里是模拟绑定了然后返回true
        return true;
    }
}
